<?php
/**
 * Desc:
 * User: hollowcg@foxmail.com
 * Date: 2019/5/25
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Menu extends Common
{
    /**
     * 菜单页面
     * @return array|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index(){

        if (Request::instance()->isAjax()){
            return \app\common\model\Menu::getDataTree();
        }

        return view();
    }

    public function add(){
        $id = input('id');
        $this->assign('pid',$id);
        $menu = \app\common\model\Menu::getDataTree();
        $this->assign('menu',$menu['data']);
        return view();
    }

    public function edit(){
        $id = input('id');

        $menu = \app\common\model\Menu::getDataTree();
        $this->assign('menu',$menu['data']);

        $data = Db::name('menu')->find($id);
        $this->assign('data',$data);
        return view();
    }

    public function save(){
        $data = input();
        return \app\common\model\Menu::saveData($data,isset($data['id'])?$data['id']:'');
    }

    public function del(){
        $id = input('id');
        $is_zi_menu = Db::name('menu')->where('pid',$id)->select();

        if(count($is_zi_menu) > 0){
            $this->error('改菜单下有子菜单，无法删除');
        }

        $res = model('menu')->destroy($id);
        if($res !== false){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
}